const path = require('path');

module.exports = {
  css: {
    modules: false,
    extract: true,
  },
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false,
  devServer: {
    port: 7000,
    open: true,
  },
  chainWebpack: config => {
    // css、js打包配置
    const splitOptions = config.optimization.get('splitChunks');
    config.optimization.splitChunks(
      Object.assign({}, splitOptions, {
        // （缺省值5）按需加载时的最大并行请求数
        maxAsyncRequests: 5,
        // （默认值3）入口点上的最大并行请求数
        maxInitialRequests: 5,
        // （默认值：1）分割前共享模块的最小块数
        minChunks: 1,
        // （默认值：30000）块的最小大小
        minSize: 30000,
        // webpack 将使用块的起源和名称来生成名称: `vendors~main.js`,如项目与"~"冲突，则可通过此值修改，Eg: '-'
        automaticNameDelimiter: '~',
        // cacheGroups is an object where keys are the cache group names.
        name: true,
        cacheGroups: {
          default: false,
          // *.vue css抽离打包为一个文件
          styles: {
            name: 'styles',
            test: (m, c) => m.constructor.name === 'CssModule' && /\.css$/i.test(m._identifier),
            chunks: 'all',
            enforce: true,
          },
          vuecss: {
            name: 'vuecss',
            test: (m, c) => m.constructor.name === 'CssModule' && !/\.css$/i.test(m._identifier),
            chunks: 'all',
            enforce: true,
          },
          vendors: {
            name: 'vendors',
            test: /[\\/]node_modules[\\/](?!(vue))/,
            priority: 1,
            chunks: 'initial',
          },
          vue: {
            name: 'vue',
            test: /[\\/]node_modules[\\/]vue/,
            chunks: 'initial',
            // 默认组的优先级为负数，以允许任何自定义缓存组具有更高的优先级（默认值为0）
            priority: 0,
          },
          app: {
            name: 'app',
            test: /[\\/]src[\\/]/,
            priority: -20,
            chunks: 'initial',
          },
        },
      }),
    );
  },
  pluginOptions: {
    // 注入less参数化全局文件
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [path.resolve(__dirname, 'src/styles/params.less')],
    },
  },
  pwa: {
    themeColor: '#7ED321',
  },
};
